package com.mall.b2c.domain.order;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mall.b2c.domain.store.StoreDomain;
import org.apache.commons.lang3.StringUtils;

import com.mall.b2c.domain.goods.GoodsDomain;
import com.mall.b2c.domain.member.CommentDomain;
import com.mall.b2c.enums.OrderItemType;
import com.mall.common.domain.BaseEntity;
import com.mall.common.util.UploadUtil;

/**
 * 订单项
 * 
 * @author wangzx
 * 
 */
@Entity
@Table(name = "b2c_order_item")
public class OrderItemDomain extends BaseEntity {
	private static final long serialVersionUID = 1320512292825711023L;

	private String productName;// 商品名称
	private String productCode;// 商品编号
	private String image;// 商品图片
	private String specification;// 规格
	private BigDecimal salePrice;// 价格
	private BigDecimal weight; // 重量
	private Integer quantity;// 数量

	private String batch;// 批次号
	private Date manufacturerDate; // 生产日期
	private Date validDate;// 有效日期
	private OrderItemType itemType;// 订单项类型

	private Integer shippedQuantity;// 已发货数量

	private OrderDomain order;
	private GoodsDomain goods;
	private CommentDomain comment;
	private RefundDomain refund;

	@PrePersist
	public void prePersist() {
		if (shippedQuantity == null) {
			shippedQuantity = 0;
		}
	}

	/**
	 * 图片全路径
	 * 
	 * @return
	 */
	@Transient
	public String getImagePath() {
		if (StringUtils.isBlank(image)) {
			return image;
		}
		return UploadUtil.replacePath(image);
	}

	/**
	 * 获取订单项商品总重
	 * 
	 * @return
	 */
	@Transient
	public BigDecimal getTotalWeight() {
		if (weight == null || quantity == null) {
			return BigDecimal.ZERO;
		}
		return BigDecimal.valueOf(quantity).multiply(weight);
	}

	/**
	 * 下单时订单项小计
	 * 
	 * @return
	 */
	@Transient
	public BigDecimal getSubtotal() {
		if (salePrice == null || quantity == null) {
			return BigDecimal.ZERO;
		}
		return salePrice.multiply(new BigDecimal(quantity));
	}

	public BigDecimal getSalePrice() {
		return salePrice;
	}

	public void setSalePrice(BigDecimal salePrice) {
		this.salePrice = salePrice;
	}

	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	@Enumerated
	@Column(nullable = false)
	public OrderItemType getItemType() {
		return itemType;
	}

	public void setItemType(OrderItemType itemType) {
		this.itemType = itemType;
	}

	@ManyToOne(fetch = FetchType.LAZY,  cascade = {CascadeType.ALL})
	@JoinColumn(name = "order_id")
	public OrderDomain getOrder() {
		return order;
	}

	public void setOrder(OrderDomain order) {
		this.order = order;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "goods_id")
	public GoodsDomain getGoods() {
		return goods;
	}

	public void setGoods(GoodsDomain goods) {
		this.goods = goods;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getBatch() {
		return batch;
	}

	public void setBatch(String batch) {
		this.batch = batch;
	}

	public Date getManufacturerDate() {
		return manufacturerDate;
	}

	public void setManufacturerDate(Date manufacturerDate) {
		this.manufacturerDate = manufacturerDate;
	}

	public Date getValidDate() {
		return validDate;
	}

	public void setValidDate(Date validDate) {
		this.validDate = validDate;
	}

	public String getProductCode() {
		return productCode;
	}

	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}

	public String getSpecification() {
		return specification;
	}

	public void setSpecification(String specification) {
		this.specification = specification;
	}

	public Integer getShippedQuantity() {
		return shippedQuantity;
	}

	public void setShippedQuantity(Integer shippedQuantity) {
		this.shippedQuantity = shippedQuantity;
	}

	public BigDecimal getWeight() {
		return weight;
	}

	public void setWeight(BigDecimal weight) {
		this.weight = weight;
	}

	@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
	@JoinColumn(name = "comment_id")
	public CommentDomain getComment() {
		return comment;
	}

	public void setComment(CommentDomain comment) {
		this.comment = comment;
	}

	@OneToOne(fetch = FetchType.LAZY)
	public RefundDomain getRefund() {
		return refund;
	}

	public void setRefund(RefundDomain refund) {
		this.refund = refund;
	}
}